package cn.newgrand.netcall.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.component.RolloverButton;
import org.jivesoftware.spark.ui.ChatRoom;
import org.jivesoftware.spark.ui.ChatRoomClosingListener;
import org.jivesoftware.spark.ui.rooms.ChatRoomImpl;
import org.jivesoftware.spark.util.log.Log;

import cn.newgrand.smackx.specialist.ExpertManager;
import cn.newgrand.smackx.specialist.packet.QuestionnaireIQ;

public class QuestionnaireRoomDecorator implements ActionListener,ChatRoomClosingListener{
	
	private ChatRoom chatRoom;
	private RolloverButton questionnaireBtn;
	
	public QuestionnaireRoomDecorator(ChatRoom chatRoom){
		this.chatRoom = chatRoom;
		questionnaireBtn = new RolloverButton(SparkRes.getImageIcon(SparkRes.COPY_16x16));
		questionnaireBtn.setToolTipText("发送一份调查问卷");
		questionnaireBtn.addActionListener(this);
		
		final JLabel dividerLabel = new JLabel(SparkRes.getImageIcon("DIVIDER_IMAGE"));
        chatRoom.getEditorBar().add(dividerLabel);
        chatRoom.getEditorBar().add(questionnaireBtn);
		
        chatRoom.addClosingListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		//发送调查问卷前判断是否符合发送的条件
		
		QuestionnaireIQ questionnarieIQ = new QuestionnaireIQ();
		String askerjid = "";
		String askername = "";
		if(chatRoom instanceof ChatRoomImpl){
			ChatRoomImpl chatRoomImpl = (ChatRoomImpl)chatRoom;
			askername = chatRoomImpl.getParticipantNickname();
			askerjid = chatRoomImpl.getJID();
			if(askerjid.lastIndexOf("/") == -1){
				askerjid += "/netcall";
			}
			questionnarieIQ.setAskerJid(askerjid);
		}
		questionnarieIQ.setSpJid(SparkManager.getSessionManager().getJID());
		questionnarieIQ.setTo(ExpertManager.SERVICE + SparkManager.getConnection().getServiceName());
		PacketCollector collector = SparkManager.getConnection().createPacketCollector(new PacketIDFilter(questionnarieIQ.getPacketID()));
		SparkManager.getConnection().sendPacket(questionnarieIQ);
		QuestionnaireIQ result = (QuestionnaireIQ)collector.nextResult(5000);
		collector.cancel();
		if(result != null && result.getError() == null){
			String res = result.getResult();
			if("success".equals(res)){
				JOptionPane.showMessageDialog(chatRoom, "调查问卷发送成功", "消息对话框", JOptionPane.PLAIN_MESSAGE);
			}else if("repeat".equals(res)){
				JOptionPane.showMessageDialog(chatRoom, "不能重复给"+askername+"发送调查问卷", "消息对话框", JOptionPane.WARNING_MESSAGE);
			}else if("nocurrent".equals(res)){
				JOptionPane.showMessageDialog(chatRoom, askername+"不是当前的咨询者,不能发送问卷", "消息对话框", JOptionPane.WARNING_MESSAGE);
			}else if("empty".equals(res)){
				JOptionPane.showMessageDialog(chatRoom, "您的专家队列为空,不能发送调查问卷", "消息对话框", JOptionPane.WARNING_MESSAGE);
			}else if("offline".equals(res)){
				JOptionPane.showMessageDialog(chatRoom, "您未以专家身份上线,不能发送调查问卷", "消息对话框", JOptionPane.WARNING_MESSAGE);
			}
		}else{
			Log.error(SparkManager.getSessionManager().getJID()+"发送调查问卷错误.");
		}
	}
	
	@Override
	public void closing() {
		chatRoom.removeClosingListener(this);
		questionnaireBtn.removeActionListener(this);
	}

}
